package com.otitan.la.forest.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.otitan.la.forest.system.dto.DicDTO;
import com.otitan.la.forest.system.entity.DicEntity;
import com.otitan.la.forest.system.service.DicService;
import com.otitan.la.forest.system.vo.DicVO;
import com.otitan.webapp.framework.basepro.model.DataResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 字典表
 *
 * @author jiacs
 * @email xxx
 * @date 2020-09-17 12:53:59
 */
@Slf4j
@Api(tags = "字典表API")
@RestController
@RequestMapping("/dic")
public class DicController {

    @Autowired
    private DicService dicService;


    @ApiOperation(value = "新增")
    @PostMapping
    public DataResult add(@Valid @RequestBody DicEntity dic, BindingResult bindingResult) {
        return dicService.add(dic, bindingResult);
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    public DataResult batchDelete(@RequestBody @ApiParam(value = "ID集合") List<String> ids) {
        return dicService.batchDelete(ids);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    public DataResult delete(@PathVariable("id") String id) {
        List<String> ids = new ArrayList<String>() {{
            add(id);
        }};
        return dicService.batchDelete(ids);
    }

    @ApiOperation(value = "更新")
    @PutMapping
    public DataResult editor(@Valid @RequestBody DicEntity dic, BindingResult bindingResult) {
        return dicService.editor(dic, bindingResult);
    }

    @ApiOperation(value = "列表查询（分页）")
    @PostMapping("/findPage/{page}/{size}")
    public DataResult<Page<DicVO>> findPageData(@PathVariable("page") long page,
                                                @PathVariable("size") long size,
                                                @RequestBody DicDTO dic) {
        return dicService.findPageData(page, size, dic);
    }

    @ApiOperation(value = "根据ID查询")
    @GetMapping("/findOne/{id}")
    public DataResult<DicEntity> findOne(@PathVariable("id") String id) {
        return dicService.findOne(id);
    }

    @GetMapping("/getOptions")
    @ApiOperation(value = "根据字典类型编码，获取字典值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dicTypeCodes", required = true, value = "字典类型编码（例如：SEX,BUTTON_TYPE）")
    })
    public DataResult getOptions(String dicTypeCodes) {
        return dicService.getOptions(dicTypeCodes);
    }

}
